const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
//const url = "https://www.kyz66.com/fubaihui/";
//const url2 = "https://xcx.kyz66.com/api/";
// const url = "https://kyz.fbhkj.com/fubaihui/";
const url = "http://192.168.31.22:30001/fubaihui/";
const url2 = "https://xcx.kyz66.com/api/";//不用动
//登录
function wxLogin(){
  let that = this;
  wx.login({
    success (res) {
      if (res.code) {
        that.setKey('code',res.code);
      } else {
        console.log('登录失败！' + res.errMsg)
      }
    }
  })
}

//时间戳转换成日期时间
function js_date_time(unixtime) {
  var dateTime = new Date(parseInt(unixtime) * 1000)
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();
  var hour = dateTime.getHours();
  var minute = dateTime.getMinutes();
  var second = dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString()); //typescript转换写法
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
  return timeSpanStr;
}

//验证手机号
function mobileReg(phone){
  var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
  if (!myreg.test(phone)) {
      return false;
  }
  return true;
}

//设置缓存
function setKey(key,value) {
  wx.setStorageSync(key, value)
}

//获取缓存
function getKey(key) {
  return wx.getStorageSync(key);
}

function wxMessage(message='',type="success") {
  wx.showToast({
    title: message,
    icon: type,
    duration: 1500,
  })
}

//获取手机号
function getPhone(){
  wx.request({
    url: url + 'app/getUserInfo',
    data:{
      storeId:home.getKey('storeId'),
      phone:'13150115204'
    },
    method: 'GET',
    header: {
      "content-type": "application/json", // 默认
    },
    success:function (res) {
      
    }
  })
}

//获取关注公众号url
function getGzUrl(){
  wx.request({
    url: url + 'app/getGzUrl',
    data:{
      storeId:home.getKey('storeId')
    },
    method: 'GET',
    header: {
      "content-type": "application/json", // 默认
    },
    success:function (res) {
      if (res.data.code == 200) {
        if (data.gzUrl) {
          home.setKey('gzUrl',data.gzUrl);
        }
      } else {
        wx.showToast({
          title: '获取关注链接失败',
          icon: 'none'
        })
      }
    }
  })
}

module.exports = {
  formatTime: formatTime,
  home: url,
  js_date_time: js_date_time,
  mobileReg:mobileReg,
  setKey:setKey,
  getKey:getKey,
  wxLogin:wxLogin,
  home2:url2,
  wxMessage:wxMessage,
  getPhone:getPhone,
  getGzUrl:getGzUrl
}